import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lz_app_core/utils/kd_storage.dart';
import 'package:lz_base/kd_base.dart';
import 'package:lz_login/src/router/kd_login_router.dart';
import 'package:lz_login/src/widgets/kd_pwd_warm_widget.dart';
import 'package:lz_router/kd_router.dart';
import 'package:lz_ui/kd_ui.dart';
import 'package:get/get.dart';
import '../serve/kd_login_api.dart';

/// 修改手机
class KDChangePwdPage extends StatefulWidget {
  const KDChangePwdPage({super.key});

  @override
  State<KDChangePwdPage> createState() => _KDChangePwdPageState();
}

class _KDChangePwdPageState extends State<KDChangePwdPage> {
  final RxBool pwdErr = false.obs;
  final Rx<KDPwdWarmType> _pwdWarmType = KDPwdWarmType.none.obs;
  TextEditingController pwdController = TextEditingController();
  TextEditingController rePwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return KDScaffold.scaffold(
      title: "修改密码",
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.all(KD.size(32)),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              KDAccountPwdWidget(
                title: "原密码",
                isPwd: true,
                textController: pwdController,
              ),
              Obx(
                () => KDAccountPwdWidget(
                  title: "新密码",
                  isPwd: true,
                  textController: rePwdController,
                  error: rePwdController.text.length < 6
                      ? "密码长度需6-20位"
                      : "请输入大于10位数密码或使用数字、字母、特殊字符号组合",
                  isErr: pwdErr.value,
                  errorWidget: pwdErr.value
                      ? null
                      : Obx(() => KDPwdWarmWidget(_pwdWarmType.value)),
                  onChange: KDInputDebounce((e) {
                    if (e.length >= 6) {
                      pwdErr.value = false;
                      _pwdWarmType.value = KDPwdWarmType.getType(e);
                    } else {
                      pwdErr.value = false;
                      _pwdWarmType.value = KDPwdWarmType.none;
                    }
                  }, 100),
                ),
              ),
              KDBgWidget(
                bgColor: KDColor.main,
                margin: EdgeInsets.zero,
                height: KD.size(80),
                onTap: next,
                child: Center(
                    child: Text(
                  "完成",
                  style: TextStyle(color: Colors.white, fontSize: KD.font(32)),
                )),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 按照dp
  Widget _space({double height = 16}) => SizedBox(
        height: KD.size(height),
      );

  next() async {
    if (pwdController.text.isEmpty) {
      DialogUtils.showToast("请输入原密码");
      return;
    }
    if (rePwdController.text.length < 6 ||
        _pwdWarmType.value == KDPwdWarmType.weak) {
      pwdErr.value = true;
      return;
    }

    bool success = await KDLoginApi.appUpdatePassword(
        oldPassword: pwdController.text, password: rePwdController.text);
    if (success) {
      DialogUtils.showToast("修改成功，请重新登陆");
      KDStorage.save(KDStorageKey.pwd.name, rePwdController.text);
      KDRouter.offAllNamed(KDLoginRouter.login);
    }
  }
}
